package com.hk.service.impl;

import com.hk.dao.ProductMenuDao;
import com.hk.model.Document;
import com.hk.model.ProductMenu;
import com.hk.service.IProductMenuService;
import com.hk.utils.Io;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: KangWang
 * Date: 2021/5/24 11:01
 * State：产品菜单业务逻辑层
 */
public class ProductMenuServiceImpl implements IProductMenuService {

    List<ProductMenu> productMenus = null;

    /**
     * 产品菜单
     * @param path
     * @param parentId
     * @return
     */
    @Override
    public List<ProductMenu> findByParentId(String path, int parentId) {
        ProductMenuDao productMenuDao = new ProductMenuDao();
//      获取所有菜单
        productMenus = productMenuDao.findByParentId();
//      1、存储路径  2、文件夹 3、文件
        Document document = new Document(path,"ProductMenu", "ProductMenu.txt");
//        检索文件夹和文件或创建并写入数据
        writeData(productMenus,document);
//        读取数据
        productMenus = (List<ProductMenu>) readData(document);
//        存储一二级关联菜单
        List<ProductMenu> productMenus1 = new ArrayList<>(20);
        if (productMenus == null && productMenus.isEmpty()) {
            System.out.println("产品菜单无数据");
            return null;
        }
        //                获取一级菜单
        for (ProductMenu productMenu : productMenus) {
            if (parentId == productMenu.getParentId()) {
                productMenus1.add(productMenu);
            }
        }
//            查询子级
        for (ProductMenu productMenu : productMenus1) {
//                每次创建一个新对象
            List<ProductMenu> productMenus2 = new ArrayList<>(5);
            for (ProductMenu menu : productMenus) {
//                    判断当前目录是否为父子关系
                if (productMenu.getMenuId() == menu.getParentId()) {
                    productMenus2.add(menu);
                }
            }
            productMenu.setProductMenus(productMenus2);
        }
        return productMenus1;
    }

    /**
     * 读取数据
     * @param document
     * @return
     */
    @Override
    public List<?> readData(Document document) {
        //    根据该对象内多少属性进行转化划分
        final int count = 4;
        productMenus = new ArrayList<>(20);
        List<String> list = Io.readData(document);
        for (int i = 0; i < list.size(); i = i+count) {
            ProductMenu productMenu = new ProductMenu(
                    Integer.parseInt(list.get(i)),
                    list.get(i+1),
                    list.get(i+2),
                    Integer.parseInt(list.get(i+3))
            );
            productMenus.add(productMenu);
        }
        return productMenus;
    }

    /**
     * 写入数据
     * @param list
     * @param document
     */
    @Override
    public void writeData(List<?> list, Document document) {
        productMenus = (List<ProductMenu>) list;
        List<String> list1 = new ArrayList<>(70);
        for (ProductMenu productMenu : productMenus) {
            list1.add(String.valueOf(productMenu.getMenuId()));
            list1.add(productMenu.getMenuName());
            list1.add(productMenu.getMenuUrl());
            list1.add(String.valueOf(productMenu.getParentId()));
        }
        Io.writeData(list1,document);

    }
}
